package com.dq.domain.orders.info;

import com.dq.vo.baseVo.VO;
import com.dq.domain.activity.Activity;
import com.dq.domain.base.BaseEntity;
import com.dq.domain.goods.Goods;
import com.dq.domain.goods.Product;
import com.dq.domain.orders.Orders;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import java.math.BigDecimal;

import static com.dq.vo.baseVo.VO.inCaseNull;


/**
 * @author cf
 */
@Data
@Entity
@ApiModel("订单详情")
public class OrderInfo extends BaseEntity implements VO.ToVO {
    @ManyToOne
    @ApiModelProperty("订单")
    protected Orders orders;

    @ManyToOne
    @ApiModelProperty("产品")
    protected Product product;
    @ManyToOne
    @ApiModelProperty("商品")
    protected Goods goods;
    @ApiModelProperty("产品个数")
    protected Long number;
    @Column(columnDefinition = "decimal(20,2)")
    @ApiModelProperty("子订单总价格")
    protected BigDecimal amount;

    @ManyToOne
    @ApiModelProperty("活动")
    protected Activity activity;


    public OrderInfo(Orders orders, Product product, Long number, BigDecimal amount,Goods goods) {
        this.orders = orders;
        this.product = product;
        this.number = number;
        this.amount = amount;
        this.goods = goods;
    }
    public OrderInfo(){

    }

    public OrderInfo(Product product,Long number,BigDecimal amount){
        this.product = product;
        this.number = number;
        this.amount = amount;
    }

    public OrderInfo(Orders orders, Activity activity, Long number, BigDecimal amount) {
        this.orders = orders;
        this.activity = activity;
        this.number = number;
        this.amount = amount;
    }

    @Data
    @ApiModel(value = "用户订单详情视图")
    public static class Vo implements VO {
        @ApiModelProperty("产品视图")
        private Product.Vo productVo;
        @ApiModelProperty("购买个数")
        private Long number;
        @ApiModelProperty("子订单总价格")
        private BigDecimal amount;
        @ApiModelProperty("商品")
        private Goods.Vo goodsVo;
        @ApiModelProperty("商品价格")
        private BigDecimal price;
        @ApiModelProperty("商品图片")
        private String listImgUrl;
        @ApiModelProperty("sku")
        private String sku;
        @ApiModelProperty("商品名称")
        private String goodName;
        private Long productId;
        private Long goodsId;
    }

    @Data
    @ApiModel("门店端订单商品详情")
    public static class OrgOrderInfoVo{
        protected Long id;
        @ApiModelProperty("商品ID")
        protected Long goodsId;
        @ApiModelProperty("产品id")
        protected Long productId;
        @ApiModelProperty("商品编号")
        protected String goodsNo;
        @ApiModelProperty("商品图片")
        protected String goodsImage;
        @ApiModelProperty("商品名称")
        protected String goodsName;
        @ApiModelProperty("商品原价")
        protected BigDecimal originalPrice;
        @ApiModelProperty("商品支付价")
        protected BigDecimal payPrice;
        @ApiModelProperty("数量")
        protected Long number;
        @ApiModelProperty("价格")
        protected BigDecimal amount;
        @ApiModelProperty("规格sku")
        protected String sku;

        @ApiModelProperty("分类名称")
        private String categoryName;
        @ApiModelProperty("门店商品库存")
        protected Long goodsStock;
    }

    public OrgOrderInfoVo toOrgOrderInfoVo(){
        OrderInfo.OrgOrderInfoVo orgOrderInfoVo = new OrderInfo.OrgOrderInfoVo();
        orgOrderInfoVo.id = this.getId();
        orgOrderInfoVo.goodsNo = fmt(this.getProduct().getGoods().getGoodsNo());
        orgOrderInfoVo.goodsId = fmt(this.getProduct().getGoods().getId());
        orgOrderInfoVo.productId = fmt(this.getProduct().getId());
        orgOrderInfoVo.goodsImage = fmt(this.getProduct().getGoods().getSmallImgUrl());
        orgOrderInfoVo.goodsName = fmt(this.getProduct().getGoods().getGoodsName());
        orgOrderInfoVo.originalPrice = fmt(this.getProduct().getMarketPrice());
        orgOrderInfoVo.payPrice = fmt(this.getProduct().getPrice());
        orgOrderInfoVo.number = fmt(this.getNumber());
        orgOrderInfoVo.amount = fmt(this.getAmount());
        orgOrderInfoVo.sku = fmt(this.getProduct().getSku());

        orgOrderInfoVo.categoryName = this.getGoods() == null ? "" : this.getGoods().getCategory().getTitle();

        return orgOrderInfoVo;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.number = fmt(this.number);
        vo.amount = fmt(this.amount);
        vo.goodsVo = isRecursion ? (Goods.Vo) inCaseNull(this.product.getGoods(), false) : null;
        vo.productVo = isRecursion ? (Product.Vo) inCaseNull(this.product, false) : null;
        vo.price = this.product != null ? fmt(this.product.getPrice()) : BigDecimal.ZERO;
        vo.listImgUrl = fmt(this.product != null ? fmt(this.product.getGoods().getSmallImgUrl()) : "");
        vo.sku = this.product != null ? fmt(this.product.getSku()) : "";
        vo.goodName = this.product != null ? fmt(this.product.getGoods().getGoodsName()) : "";
        vo.productId = this.product != null?fmt(this.getProduct().getId()):null;
        vo.goodsId = this.product != null  ? fmt(this.product.getGoods().getId()) : null;
        return vo;
    }

}
